package com.hongdee.scm.wareHouse.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.wareHouse.entity.TScmWarehouse;
import com.hongdee.scm.wareHouse.service.ITScmWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 名  称:TScmWarehouseController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.wareHouse.controller
 * 作  者: 术洪超
 * 日  期:2017-04-28 11:26
 */

@RestController
@RequestMapping("/scm/warehouse")
public class TScmWarehouseController {

    @Autowired
    private ITScmWarehouseService itScmWarehouseService;


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  参数数组
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmWarehouseService.page(start, limit, uid, data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * @param data 仓库实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmWarehouse data) throws Exception {
        TScmWarehouse wh = this.itScmWarehouseService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(wh);
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择仓库！");
        }
        Map map = itScmWarehouseService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 启用仓库
     * @author 术洪超
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmWarehouseService.start(id);
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 停用仓库
     * @author 术洪超
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmWarehouseService.stop(id);
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }


    /**
     * @return List
     * @throws Exception
     * @desc 仓库下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(String uid) throws Exception {
        return this.itScmWarehouseService.combo(uid);
    }


    /**
     * @return List
     * @throws Exception
     * @desc 仓库下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/comboIn")
    @ResponseBody
    public List<Map> comboIn(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody String[] companyId
    ) throws Exception {
        return this.itScmWarehouseService.comboIn(uid, companyId);
    }


    /**
     * @param uid 登录用户 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询公司地址，用于添加仓库时的默认地址
     * @author 术洪超
     */

    @RequestMapping("/address")
    @ResponseBody
    public ResultEntity address(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itScmWarehouseService.address(uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc 仓库转换
     * @author 徐钦勇
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Map convert() throws Exception {
        return this.itScmWarehouseService.convert();
    }

    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc 仓库转换
     * @author 徐钦勇
     */
    @RequestMapping("/convertCode")
    @ResponseBody
    public Map convertCode() throws Exception {
        return this.itScmWarehouseService.convertCode();
    }
}
